package com.v.writable;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;


public class FlowMap extends Mapper<LongWritable, Text,Text,FlowBean> {

    private Text outK = new Text();
    private FlowBean outV = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value,
                       Mapper<LongWritable, Text, Text, FlowBean>.Context context)
            throws IOException, InterruptedException {
        //1转换成字符串
        String line = value.toString();

        //2切割
        String[] tokens = line.split("\t");

        //3将值取出，这里注意网 址会为空，因此需要倒着取出
        String phone = tokens[1];
        String up = tokens[tokens.length - 3];
        String down = tokens[tokens.length - 2];

        //4设置text 和FlowBean,
        // 这里注意上行流量和下行流量位置确定了
        // 再调用重载后的setSumFlow方法即可
        outK.set(phone);
        outV.setUpFlow(Long.parseLong(up));
        outV.setDownFlow(Long.parseLong(down));
        outV.setSumFlow();

        //5写出
        context.write(outK,outV);
    }
}
